package com.apache.rpc.tomcat;

import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultMsg;
import com.apache.cache.service.impl.LoadCacheFactory;
import com.apache.cache.util.Validator;
import com.apache.database.model.Page;
import com.apache.database.model.TaskEntity;
import com.apache.rpc.common.RpcUtil;
import com.apache.rpc.controller.RpcWelcomeAction;
import com.apache.rpc.entity.InterfaceRegister;
import com.apache.rpc.entity.RpcDatasource;
import com.apache.rpc.entity.RpcMonitoring;
import com.apache.rpc.entity.SystemConfig;
import com.apache.rpc.manager.*;
import com.apache.rpc.service.impl.helper.CacheTactics;
import com.apache.task.api.ScheduledExecutor;
import com.apache.task.util.XmlReader;
import com.apache.tools.MD5Utils;
import com.apache.tools.OsUtils;
import com.apache.tools.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * description:
 *
 * @author dyh 创建时间：2018年02月12日
 */
@Controller
public class WelcomeAction{

    /**
     * 跳到登录页
     */
    @RequestMapping(value = "/login.jsp", method = RequestMethod.GET)
    public String gotoLogin(HttpServletRequest request, Map<String, Object> data) {
        data.put("ctx", request.getContextPath());
        request.getSession().setAttribute("ctx", request.getContextPath());
        return "login";
    }

    /**
     * 退出系统
     */
    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    @ResponseBody
    public Object gotoLogout(HttpServletRequest request) {
        request.getSession().removeAttribute("userName");
        Map<String, String> result = new HashMap<String, String>(2);
        result.put("flag", "T");
        result.put("msg", "登录成功");
        return result;
    }

    /**
     * 跳到列表页
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String toIndex(HttpServletRequest request, Map<String, Object> data) {
        data.put("ctx", request.getContextPath());
        data.put("title", "RPC系统管理控制台");
        data.put("userName", "admin");
        return "/index";
    }

    /**
     * 登录
     */
    @RequestMapping(value = "/main/login", method = RequestMethod.POST)
    @ResponseBody
    public Object login(HttpServletRequest request, Map<String, Object> data) {
        data.put("ctx", request.getContextPath());
        Map<String, String> result = new HashMap<String, String>(2);
        String userName = request.getParameter("userName");
        String pwd = request.getParameter("password");
        if (StrUtil.isNull(userName) || StrUtil.isNull(pwd)) {
            result.put("flag", "F");
            result.put("msg", "用户或密码不能为空");
        } else {
            String consoleUser = RpcUtil.getInstance().getValueByKey("console.user");
            String userPwd = RpcUtil.getInstance().getValueByKey("console.user.pwd");
            String pwdMd5 = MD5Utils.MD5(MD5Utils.MD5(userPwd) + "apache_rpc").toLowerCase();
            if (!userName.equals(consoleUser) || !pwd.equals(pwdMd5)) {
                result.put("flag", "F");
                result.put("msg", "用户或密码不正确");
            } else {
                request.getSession().setAttribute("userName", userName);
//                System.out.println("sessionId=" + request.getSession().getId());
                data.put("userName", userName);
                result.put("flag", "T");
                result.put("msg", "登录成功");
            }
        }
        return result;
    }


}
